package com.mobile.tools;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;


public class SetCharacterEncodFilter implements Filter {
	private static final Logger LOGGER = LogManager.getLogger(SetCharacterEncodFilter.class);
	public void destroy() {
		// TODO Auto-generated method stub

	}
	/**
	 * 对request和response传递的参数进行编码转换
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		try {
			// TODO Auto-generated method stub
			request.setCharacterEncoding("UTF-8") ;
			response.setCharacterEncoding("UTF-8");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			LOGGER.error("编码转换出错", e);
		}	
		/**
		 *	往下传递request,response参数
		 */
		chain.doFilter(request, response);
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}